// pages/tousu/detail.js
import config from '../../utils/config.js';
const logo=config.logo;
const getWebsiteName= config.getWebsiteName;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
  checked:true,
  ck:'',
  content:'',
  logo:logo,
  getWebsiteName: getWebsiteName,
  name:'',
  replayObj: {
    status: 0,
    imageslist: [],
    imageslistforFome: [],
  },
  form: {
    content: '',
    imgs: [],
  },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.name!=undefined){
      this.setData({
        name: options.name
      })
    }
    wx.hideShareMenu({})

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  textInput:function(e){
    console.log(e.detail.value);
    this.setData({
      content:e.detail.value,
    })
  },
  chooseimg: function () {
    var _this = this;
    wx.chooseImage({
      count: 4, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;

        tempFilePaths.forEach(function (v, i) {
          //_this.oploadimg(v, i);
          var arr = [{ 'imgurl': v, 'progress': 0 }];
          var arr2 = [{ 'imgurl': v, 'progress': 0 }];
          _this.data.replayObj.imageslist = arr.concat(_this.data.replayObj.imageslist);
          _this.data.replayObj.imageslistforFome = arr2.concat(_this.data.replayObj.imageslistforFome);
          //_this.data.imageslist.push({ 'imgurl': '../../images/icon_picture.png','progress':'0'});
        })
        _this.setData({
          replayObj: _this.data.replayObj,
        })
      }
    })
  },
  deleteimg: function (e) {
    console.log(e);
    console.log(this.data.replayObj.imageslist);
    this.data.replayObj.imageslist.splice(e.currentTarget.dataset.index, 1);
    this.data.replayObj.imageslistforFome.splice(e.currentTarget.dataset.index, 1);
    console.log(this.data.replayObj.imageslist);
    this.setData({
      replayObj: this.data.replayObj,
    })

  },
  oploadimg: function (file, index) {
    var _this = this;
    wx.showLoading({
      title: '上传ing',
    })
    const uploadTask = wx.uploadFile({
      url: app.globalData.api + '/index/upload', //仅为示例，非真实的接口地址
      filePath: file,
      name: 'file',
      header: {
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        var response = JSON.parse(res.data);
        if (response.code == '200') {
          console.log(response);
          //_this.data.imageslist[index].imgurl = response.data.imgurl;
          _this.data.replayObj.imageslistforFome[index].imgurl = response.data.imgurl;
          _this.data.replayObj.imageslistforFome[index].progress = 100;
          _this.setData({
            replayObj: _this.data.replayObj,
          })
          wx.hideLoading();
        }
        else if (response.code == 401) {
          _this.data.replayObj.imageslist[index].imgurl = '../../images/error.png';
          _this.data.replayObj.imageslist[index].progress = 100;
          _this.data.replayObj.imageslistforFome[index].imgurl = '../../images/error.png';
          _this.data.replayObj.imageslistforFome[index].progress = 100;
          _this.setData({
            replayObj: _this.data.replayObj,
          })
          wx.showToast({
            title: '用户未授权,重新授权',
            success: function () {

            }
          })
        }
        else {
          _this.data.replayObj.imageslist[index].imgurl = '../../images/error.png';
          _this.data.replayObj.imageslist[index].progress = 100;
          _this.data.replayObj.imageslistforFome[index].imgurl = '../../images/error.png';
          _this.data.replayObj.imageslistforFome[index].progress = 100;
          _this.setData({
            replayObj: _this.data.replayObj,
          })
          wx.showModal({
            title: '失败',
            content: response.msg,
          })
        }

        //do something
      },
      fail: function (res) {
        if (res.data.code == 401) {
          wx.showToast({
            title: '用户未授权,重新授权',
            success: function () {

            }
          })
        }
        else {
          wx.showModal({
            title: '失败',
            content: res.msg,
          })
        }


      }
    })

    uploadTask.onProgressUpdate((res) => {
      _this.data.replayObj.imageslist[index].progress = res.progress;
      _this.setData({
        replayObj: this.data.replayObj
      })
      console.log('上传进度', res.progress)
      console.log('已经上传的数据长度', res.totalBytesSent)
      console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
    })
  },
  checkboxChange:function(e){
    this.setData({
      checked: e.detail.value,
    })
  },
  go_sucess:function(){
    if(this.data.content==''){
      wx.showToast({
        title: '内容不能为空',
        icon:'',
      });
    }
    else if (this.data.replayObj.imageslist==0) {
      wx.showToast({
        title: '请上传图片',
        icon: 'none',
      });
    }
    else{
      wx.request({
        url: app.globalData.api +'/complaint/index.html&token='+wx.getStorageSync('token'),
        method:'post',
        data:{
          content:this.data.content,
        },
        success:(res)=>{
          wx.navigateTo({
            url: '/pages/tousu/success',
          })
        }

      })

    }
  },
})